package com.zzxy.mvc.controller;

import com.zzxy.mvc.entity.Student;
import com.zzxy.mvc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2023-12-14 14:28:25
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    /*@Resource
    private StudentMapper studentMapper;*/
    /**
     * 服务对象
     */
    @Autowired
    private StudentService studentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Student selectOne(Long id) {
        return this.studentService.queryById(id);
    }

    @RequestMapping("/list")
    public String list(Model model) {
        List<Student> students = studentService.getAll();
        /*for (Student student : students) {
            System.out.println(student);
        }*/
        model.addAttribute("userList", students);
        return "/user/userList";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        //queryById
        Student student = studentService.findById(id);
        model.addAttribute("user", student);
        return "/user/userEdit";
    }

    @RequestMapping("/update")
    public String update(Student student, Model model) {
        System.out.println(student);
        studentService.update(student);
//        List<Student> students = studentService.getAll();
//        model.addAttribute("userList", students);
//        return "/user/userList";
        return "redirect:list.do";
    }
}